package com.dp.mallchat;

import com.dp.mallchat.common.MallChatCustomApplication;
import com.dp.mallchat.common.common.thread.ThreadPoolConfig;
import com.dp.mallchat.common.user.domain.user.enums.IdempotenceEnum;
import com.dp.mallchat.common.user.domain.user.enums.ItemEnum;
import com.dp.mallchat.common.user.domain.user.req.BlackReq;
import com.dp.mallchat.common.user.service.LoginService;
import com.dp.mallchat.common.user.service.UserBackpackService;
import com.dp.mallchat.common.user.service.UserService;
import com.dp.mallchat.oss.MinIOTemplate;
import com.dp.mallchat.oss.domain.OssReq;
import com.dp.mallchat.oss.domain.OssResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.junit.jupiter.api.Test;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author:
 * @CreateTime: 2024-05-15  13:40
 * @Description: TODO
 */
@SpringBootTest(classes = MallChatCustomApplication.class)
@Slf4j
public class TTTT {

    public static final Long UID = 11011L;
    @Autowired
    private LoginService loginService;
    @Autowired
    private ThreadPoolConfig threadPoolConfig;

    @Autowired
    private UserBackpackService userBackpackSqervice;
    @Autowired
    private UserService userService;

    @Test
    public void acquireItem() {
        userBackpackSqervice.acquireItem(UID, ItemEnum.PLANET.getId(), IdempotenceEnum.UID, UID.toString());
    }

    @Test
    public void testJwt() {
        String token = loginService.getToken(11003L);
        System.out.println(token);
        boolean verify = loginService.verify(token);
        System.out.println(verify);
        Long uid = loginService.getValidUid(token);
        System.out.println(uid);
    }


    @Test
    public void testThreadPool() {
        Executor asyncExecutor = threadPoolConfig.getAsyncExecutor();
        asyncExecutor.execute(() -> {
            if (true) {
                throw new RuntimeException("1234");
            }
        });
    }

    @Test
    public void testThread() {
        ExecutorService pool = Executors.newFixedThreadPool(1);
        pool.execute(() -> {
            if (true) {
                throw new RuntimeException("1234");
            }
        });
    }

    @Test
    public void testBlack() {
        BlackReq blackReq = new BlackReq();
        blackReq.setUid(UID);
        userService.black(blackReq);
    }

    private RedissonClient redissonClient;

    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        List<Integer> list2 = new ArrayList<>();
        list2.add(4);
        list2.add(1);
        List<Integer> collect = Stream.concat(list1.stream(), list2.stream())
                .distinct().collect(Collectors.toList());
        System.out.println(collect);
    }

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Test
    public void send(){
        Message<String> build = MessageBuilder.withPayload("456").build();
        rocketMQTemplate.send("test-topic", build);
    }

    @Autowired
    private MinIOTemplate minIOTemplate;

    @Test
    public void getUploadUrl() {
        OssReq ossReq = OssReq.builder()
                .fileName("1697980282409.jpg")
                .filePath("/test")
                .autoPath(true)
                .build();
        OssResp preSignedObjectUrl = minIOTemplate.getPreSignedObjectUrl(ossReq);
        System.out.println(preSignedObjectUrl);
    }
}
